//
//  DetailsViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/3/4.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "DetailsViewController.h"


#import "RKTagsMenuView.h"
#import "TodoHttpManager.h"
#import "ZhuCustomCell.h"

#import "MJExtension.h"
#import "HouseModel.h"
#import "ZhuHouseXVC.h"
#import "GouDetailsViewController.h" //红色的点进去的详情

#import "MJRefresh.h"
@interface DetailsViewController ()<UITableViewDelegate,UITableViewDataSource,ZhuCustomCellDelegate>

{
    NSString *_unitStr;
    NSMutableDictionary *_zShouDic;
    NSMutableArray *_zonArr;
    NSString *houseID;
    NSString *_colorStr;//状态,-----颜色
    
    
    
    NSString *_seleUnit;
    NSString *_newZongStr;
    NSString *_newYiStr;
    NSString *_newWeiStr;
}


@property(nonatomic,strong)UIView *aView;
@property(nonatomic,strong)NSMutableArray *zhuArr;
@property (nonatomic,strong) NSMutableArray *qiArr;
@property (nonatomic,strong) NSMutableArray *qiIDArr;
@property(nonatomic,strong)UITableView *tabView;

@property (nonatomic,strong) NSMutableArray *oneArr;

@property (nonatomic,strong) NSMutableArray *twoArr;

@property (nonatomic,strong) NSMutableArray *threeArr;
@property(nonatomic,strong)HouseModel *houModel;
@property (nonatomic,strong) NSMutableArray *floorArr;
@property(nonatomic,strong)NSMutableArray *oModelArr;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSMutableArray *changArr;
@property(nonatomic,strong)NSMutableArray *lastArr;


@end

@implementation DetailsViewController

-(void)viewWillAppear:(BOOL)animated
{
    
    //主售房源单元
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDTZhuShouHouseUnit:) name:TDTZhuShouHouseUnit object:nil];
    
    //主售房源单元
   
    if (_zhuArr.count == 0)
    {
        //TODO:主售房源单元
        [_manager UserTYZhuShouUnitWithID:_proID];
        
        
    }
    
    //主售房源信息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDFZhuShouHouseMessage:) name:TDFZhuShouHouseMessage object:nil];
    
    
}

//表头通知方法
-(void)didTDTZhuShouHouseUnit:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        int status = [[notifi.object objectForKey:@"status"] intValue];
        if (status == 1)
        {
            
            _zhuArr = [notifi.object objectForKey:@"data"];
            
            _zShouDic = [_zhuArr objectAtIndex:0];
            _unitStr = [_zShouDic objectForKey:@"unit"];
              _seleUnit = [_zShouDic objectForKey:@"unit"];
            for (int i = 0; i < _zhuArr.count; i++) {
                
                NSDictionary *dict = [_zhuArr objectAtIndex:i];
                [_qiArr addObject:dict[@"name"]];
                // [_qiIDArr addObject:dict[@"id"]];

            }
            
            [_tabView reloadData];
            [self setupMenuQiArr:_qiArr];
            [super viewDidLoad];
            
            
        }
        else
        {
#pragma mark----------------失败的加载
            [_manager MakeMyselfAlert:@"正在加载" andIsNeedTitle:NO andShowTime:5.0];
        }
    }
    
    
}

//整个页面数据的解析部分
-(void)didTDFZhuShouHouseMessage:(NSNotification *)notifi
{
    if (notifi.object !=0)
    {
        
        NSLog(@"notifi.object===%@",notifi.object);
        _newYiStr = [notifi.object objectForKey:@"sale_rooms"];
        _newZongStr = [notifi.object objectForKey:@"all_rooms"];
        _newWeiStr = [notifi.object objectForKey:@"rest_rooms"];
        self.houModel = [HouseModel mj_objectWithKeyValues:notifi.object];
        if (_floorArr.count >0)
        {
            [_floorArr removeAllObjects];
        }
        if (_dataArr.count >0)
        {
            [_dataArr removeAllObjects];
        }
        for (DataModel *dataA in self.houModel.data)
        {
           
            [self.floorArr addObject:dataA.floor];
            
           
            [self.dataArr addObject:dataA.data];
            

            
            for (DataModel2 *dataB in dataA.data)
            {
            
                [self.oModelArr addObject:dataB];

            }
            
  
        }
        
        
        
        
        [self.tabView reloadData];
        [self setupTitle];
    }
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    //移除表头通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDTZhuShouHouseUnit object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDFZhuShouHouseMessage object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"房源销售";

    _manager = [TodoHttpManager sharedManager];
    _zhuArr = [[NSMutableArray alloc] init];
    _zShouDic = [[NSMutableDictionary alloc] init];
    _qiArr = [[NSMutableArray alloc] init];
    _zonArr = [NSMutableArray arrayWithCapacity:0];
    _floorArr = [NSMutableArray array];
    _oneArr = [NSMutableArray array];
    _twoArr = [NSMutableArray array];
    _threeArr = [NSMutableArray array];
  _oModelArr = [NSMutableArray array];
    _dataArr = [NSMutableArray array];
    
    _qiIDArr = [NSMutableArray array];
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight+30+45, B_WIDTH, B_HEIGHT-(LL_StatusBarAndNavigationBarHeight+30+45)) style:UITableViewStylePlain];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 50;
    _tabView.backgroundColor = BgColor;
    _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tabView];
    [self setupTitle];
  
    
     [self setupMenuQiArr:_qiArr];
    

    
    //TODO':下拉刷新
    self.tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(zhuSHouheaderTageAction)];

}

#pragma mark----  刷新页面的方法   ------
-(void)zhuSHouheaderTageAction
{
    
       // [self refreshTab];
//    if (<#condition#>) {
//        <#statements#>
//    }
#pragma mark------刷新的时候回返回到一单元
  //  [self setupMenuQiArr:_qiArr];
    
        [self setupTitle];
        [self.tabView.mj_header endRefreshing];
}

-(NSMutableArray *)oModelArr{
    if (_oModelArr) {
        return _oModelArr;
    }
    
    _oModelArr = [[NSMutableArray alloc] initWithCapacity:0];
    return _oModelArr;
}

-(NSMutableArray *)changArr
{
    if (_changArr) {
        return _changArr;
    }
    
    _changArr = [[NSMutableArray alloc] initWithCapacity:0];
    return _changArr;
}

-(NSMutableArray *)lastArr
{
    if (_lastArr) {
        return _lastArr;
    }
    
    _lastArr = [[NSMutableArray alloc] initWithCapacity:0];
    return _lastArr;
}

-(void)setupTitle
{
   UIView * aView = [[UIView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, B_WIDTH, 30)];
    aView.backgroundColor = [UIColor orangeColor];
    //aView.backgroundColor = [UIColor colorWithHexString:@"#eb6100"];
    [self.view addSubview:aView];
    UILabel *oneLab = [[UILabel alloc] init];
    oneLab.text = [NSString stringWithFormat:@"总房源 :%@套",_newZongStr.length>0?_newZongStr:@""];
    oneLab.textColor = [UIColor whiteColor];
    oneLab.font = [UIFont systemFontOfSize:12];
    [aView addSubview:oneLab];
    [oneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 20));
        make.left.equalTo(aView.mas_left).offset(20);
        make.top.equalTo(aView.mas_top).offset(5);
        
    }];
    
    UILabel *twoLab = [[UILabel alloc] init];
    twoLab.text = [NSString stringWithFormat:@"已售 :%@套",_newYiStr.length>0?_newYiStr:@""];
    twoLab.textColor = [UIColor whiteColor];
    twoLab.font = [UIFont systemFontOfSize:12];
    [aView addSubview:twoLab];
    [twoLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 20));
        make.left.equalTo(oneLab.mas_right).offset(10);
        make.top.equalTo(aView.mas_top).offset(5);
        
    }];
    
    UILabel *sanLab = [[UILabel alloc] init];
    sanLab.text = [NSString stringWithFormat:@"未售 :%@套",_newWeiStr.length>0?_newWeiStr:@""];
    sanLab.textColor = [UIColor whiteColor];
    sanLab.font = [UIFont systemFontOfSize:12];
    [aView addSubview:sanLab];
    [sanLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 20));
        make.left.equalTo(twoLab.mas_right).offset(10);
        make.top.equalTo(aView.mas_top).offset(5);
        
    }];

    
}

-(void)setupMenuQiArr:(NSMutableArray *)qiArr//这一个方法在点击第一单元的时候出发不？
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    RKTagsMenuView *tagMenuView = [[RKTagsMenuView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight+30,self.view.frame.size.width, 44)];
    tagMenuView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tagMenuView];
    [tagMenuView setTagsArray:qiArr];
    
  //  NSLog(@"bbbb-----%@",_unitStr);
    [_manager userFYZhuShouMessageWithLouID:_proID withUnitID:_seleUnit];
    
     [self.tabView reloadData];
    [tagMenuView setDidSelectTagBlock:^(NSInteger clickedRow) {
        
        _seleUnit = qiArr[clickedRow];
        //这个是点击第一单元,然后发的请求
        if(_changArr.count!=0  ){
            [_changArr removeAllObjects];
            [self.floorArr removeAllObjects];
            [self.dataArr removeAllObjects];
            [self.oModelArr removeAllObjects];
              [self.tabView reloadData];
              [_manager userFYZhuShouMessageWithLouID:_proID withUnitID:_seleUnit];
            
        }

        
    }];
    
    
}
- (void)houseBtnClick:(NSDictionary *)dic
{
    NSLog(@"-------88899988%@----",dic);
    NSString *aaID = [dic objectForKey:@"B_TAG"];
    
//    ZhuHouseXVC *zhuVC = [[ZhuHouseXVC alloc] init];
//    zhuVC.fangID = aaID;
//    NSLog(@"=1111====%@",aaID);
//    zhuVC.Is_Sale = [dic objectForKey:@"B_SALE"];
//  
//    NSLog(@"zhuVC.Is_Sale====%@",zhuVC.Is_Sale);
//    [self.navigationController pushViewController:zhuVC animated:YES];
    
    _colorStr = [dic objectForKey:@"B_SALE"];
    if ([_colorStr isEqualToString:@"2"])//已售
    {
        GouDetailsViewController *yiShouVc = [[GouDetailsViewController alloc] init];
        yiShouVc.idStr = aaID;
        [self.navigationController pushViewController:yiShouVc animated:YES];
    }
    else
    {
        ZhuHouseXVC *zhuVC = [[ZhuHouseXVC alloc] init];
            zhuVC.fangID = aaID;
            NSLog(@"=1111====%@",aaID);
            zhuVC.Is_Sale = [dic objectForKey:@"B_SALE"];
        
            NSLog(@"zhuVC.Is_Sale====%@",zhuVC.Is_Sale);
            [self.navigationController pushViewController:zhuVC animated:YES];
    }
    
    
    
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
          
    
    
    return _floorArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellId = @"Cell";
     ZhuCustomCell *cell =  [[ZhuCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];

    if ([cell isKindOfClass:[ZhuCustomCell class]])
    {
        if (_oModelArr.count != 0 ) {
            
            DataModel2 *mode = [_oModelArr objectAtIndex:indexPath.row];
            
            cell.oneLab.text = _floorArr[indexPath.row];
            
            houseID = mode.room_id;
            self.changArr = self.houModel.data[indexPath.row].data;//
            NSMutableArray *stateArr = [NSMutableArray new];
            
            for (int i =0; i<_changArr.count; i++) {
                [stateArr addObject:[_changArr[i] is_sale]];
            }
            
            NSLog(@"stateArr===%@",stateArr);
            [(ZhuCustomCell *)cell setupCustomTableViewCellWIthBtnArr:_changArr withStatne:stateArr withShangLab:nil model:self.houModel];
            
        }

    }
    
    cell.delegate = self;
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.backgroundColor = BgColor;
   
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

       //
    
    
    NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
   // NSLog(@"=======IDA==%@",IDA);
    if ([IDA isEqualToString:@"1"])
    {
        UIView *erView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 40)];
        erView.backgroundColor = OverColor;
        [self.view addSubview:erView];
        UILabel *louLab1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 50, 20)];
        louLab1.textColor = [UIColor whiteColor];
        louLab1.text = @"楼层";
        louLab1.font = [UIFont systemFontOfSize:14];
        louLab1.textAlignment = NSTextAlignmentCenter;
        [erView addSubview:louLab1];
        
        UILabel *lineLa1 = [[UILabel alloc] initWithFrame:CGRectMake(62, 0, 2, 40)];
        lineLa1.backgroundColor = [UIColor whiteColor];
        [erView addSubview:lineLa1];
        
        UILabel *fangLab1 = [[UILabel alloc] initWithFrame:CGRectMake(63, 10, B_WIDTH-63, 20)];
        fangLab1.textColor = [UIColor whiteColor];
        fangLab1.text = @"房源";
        fangLab1.font = [UIFont systemFontOfSize:14];
        fangLab1.textAlignment = NSTextAlignmentCenter;
        [erView addSubview:fangLab1];

        return erView;
    }
    else
    {
        NSInteger aa = (B_WIDTH-300)/4;
        
        UIView *oneView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 70)];
        oneView.backgroundColor = BgColor;
        [self.view addSubview:oneView];
        
        UIButton *redBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        redBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        [redBtn setTitleColor:[UIColor blackColor] forState:0];
        [redBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:7];
        [redBtn setImage:[UIImage imageNamed:@"Color_yishou"] forState:0];
        [redBtn setTitle:@"已售" forState:0];
        [oneView addSubview:redBtn];
        [redBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(55, 20));
            make.left.equalTo(oneView.mas_left).offset(6);
            make.top.equalTo(oneView.mas_top).offset(5);
        }];
        
        UIButton *writeBrn = [UIButton buttonWithType:UIButtonTypeCustom];
        writeBrn.titleLabel.font = [UIFont systemFontOfSize:10];
        [writeBrn setTitleColor:[UIColor blackColor] forState:0];
        [writeBrn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:7];
        [writeBrn setImage:[UIImage imageNamed:@"Color_bai"] forState:0];
        [writeBrn setTitle:@"未售" forState:0];
        [oneView addSubview:writeBrn];
        [writeBrn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(55, 20));
            make.left.equalTo(redBtn.mas_right).offset(aa);
            make.top.equalTo(oneView.mas_top).offset(5);
        }];
        
        UIButton *blueBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        blueBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        [blueBtn setTitleColor:[UIColor blackColor] forState:0];
        [blueBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:7];
        [blueBtn setImage:[UIImage imageNamed:@"Color_blue"] forState:0];
        [blueBtn setTitle:@"抵押融资" forState:0];
        [oneView addSubview:blueBtn];
        [blueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(70, 20));
            //make.left.equalTo(oneView.mas_left).offset(6);
            make.top.equalTo(oneView.mas_top).offset(5);
            make.centerX.equalTo(oneView.mas_centerX);
        }];
        
        
        UIButton *greenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        greenBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        [greenBtn setTitleColor:[UIColor blackColor] forState:0];
        [greenBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:7];
        [greenBtn setImage:[UIImage imageNamed:@"Color_lv"] forState:0];
        [greenBtn setTitle:@"内留" forState:0];
        [oneView addSubview:greenBtn];
        [greenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(50, 20));
            make.left.equalTo(blueBtn.mas_right).offset(aa);
            make.top.equalTo(oneView.mas_top).offset(5);
            
        }];
        
        
        UIButton *grayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        grayBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        [grayBtn setTitleColor:[UIColor blackColor] forState:0];
        [grayBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:7];
        [grayBtn setImage:[UIImage imageNamed:@"Color_zi"] forState:0];
        [grayBtn setTitle:@"调房待批" forState:0];
        [oneView addSubview:grayBtn];
        [grayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(70, 20));
            make.right.equalTo(oneView.mas_right).offset(-6);
            make.top.equalTo(oneView.mas_top).offset(5);
            
        }];
        
        
        
        UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 30, B_WIDTH, 40)];
        bView.backgroundColor = OverColor;
        [oneView addSubview:bView];
        UILabel *louLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 50, 20)];
        louLab.textColor = [UIColor whiteColor];
        louLab.text = @"楼层";
        louLab.font = [UIFont systemFontOfSize:14];
        louLab.textAlignment = NSTextAlignmentCenter;
        [bView addSubview:louLab];
        
        UILabel *lineLa = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 2, 40)];
        lineLa.backgroundColor = [UIColor whiteColor];
        [bView addSubview:lineLa];
        
        UILabel *fangLab = [[UILabel alloc] initWithFrame:CGRectMake(63, 10, B_WIDTH-63, 20)];
        fangLab.textColor = [UIColor whiteColor];
        fangLab.text = @"房源";
        fangLab.font = [UIFont systemFontOfSize:14];
        fangLab.textAlignment = NSTextAlignmentCenter;
        [bView addSubview:fangLab];
        
        

        return oneView;
    }

    
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //这个是每一个楼层有几个房间
    NSInteger number = 1;
   // NSLog(@"change.count===%ld",_changArr.count);
    
    
     self.changArr = self.houModel.data[indexPath.row].data;

    if (self.houModel.data[indexPath.row].data.count %3 == 0)
    {
        number =(_changArr.count)/3;
    }
    else
    {
        if (_changArr.count /3.0f < 1)
        {
            number = 1;
        }
        else
        {
            number = _changArr.count/3 + 1;
        }
    }
    

 
    
    return number *42.f;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
   // NSLog(@"=======IDA==%@",IDA);
    if ([IDA isEqualToString:@"1"])
    {
        return 40;
    }
    else
    {
     return 70;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
